﻿/******************************************************************************
 *  Project:		Neddle Community Edition
 *  Filename:		DataObject.cs
 *	Created By:     Kristopher Cargile <kristopher@neddle.org>
 *  Date Created:	March 24, 2011
 * ****************************************************************************
 * IMPORTANT GNU LICENSE INFORMATION. DO NOT REMOVE THIS SECTION!
 * 
 * Neddle :: Copyright (c) 2010-2012, Cargile Technology Group, LLC
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ******************************************************************************/

using System;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Xml.Serialization;

namespace Neddle
{
    /// <summary>
    /// Supports all data-bound classes in Neddle and provides basic application level services.
    /// </summary>
    [Serializable]
    public abstract class DataObject
    {
        /// <summary>
        /// The default "system" user id.
        /// </summary>
        public const string DefaultSystemUserId = "SYSTEM";

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        [XmlElement("id")]
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the permalink / unique id.
        /// </summary>
        /// <value>The permalink.</value>
        [XmlElement("permalink")]
        public Guid Permalink { get; set; }

        /// <summary>
        /// Gets or sets the date the entity was created.
        /// </summary>
        /// <value>The create date.</value>
        [XmlElement("created")]
        public DateTime Created { get; set; }

        /// <summary>
        /// Gets or sets the identity of the user who created the entity.
        /// </summary>
        /// <value>The username.</value>
        [XmlElement("createdBy")]
        public string CreatedBy { get; set; }

        /// <summary>
        /// Gets or sets the date the entity was last modified.
        /// </summary>
        /// <value>The last modified date.</value>
        [XmlElement("modified")]
        public DateTime Modified { get; set; }

        /// <summary>
        /// Gets or sets the identity of the user who last modified the entity.
        /// </summary>
        /// <value>The username.</value>
        [XmlElement("modifiedBy")]
        public string ModifiedBy { get; set; }

        /// <summary>
        /// Gets or sets the row version.
        /// </summary>
        /// <value>The row version.</value>
        [Timestamp]
        [XmlElement("rowVersion", IsNullable = true)]
        public byte[] RowVersion { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataObject"/> class.
        /// </summary>
        protected DataObject()
        {
            Created = Modified = DateTime.Now;
            CreatedBy = ModifiedBy = DefaultSystemUserId;
            Permalink = Guid.NewGuid();

            if (!string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name))
            {
                CreatedBy = ModifiedBy = Thread.CurrentPrincipal.Identity.Name;
            }
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public bool Equals(DataObject obj)
        {
            return base.Equals(obj) &&
                   Created == obj.Created &&
                   CreatedBy == obj.CreatedBy &&
                   Id == obj.Id &&
                   Modified == obj.Modified &&
                   ModifiedBy == obj.ModifiedBy &&
                   Permalink == obj.Permalink &&
                   RowVersion == obj.RowVersion;
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (!(obj is DataObject))
            {
                return false;
            }

            if (ReferenceEquals(obj, this))
            {
                return true;
            }

            return Equals(obj as DataObject);
        }
    }
}
